package com.itedge.solutionmanager.service.task.impl.customer;

import org.activiti.engine.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itedge.solutionmanager.domain.impl.Customer;
import com.itedge.infrastructure.service.domain.IProcessEntityService;
import com.itedge.infrastructure.service.task.ITaskService;
import com.itedge.infrastructure.service.taskdata.ITaskDataEntityService;
import com.itedge.solutionmanager.taskdata.impl.customer.EditCustomerTaskData;
import com.itedge.solutionmanager.util.StringUtil;

@Service
public class EditCustomerTaskService implements ITaskService<EditCustomerTaskData, Customer> {

    @Autowired
    protected TaskService taskService;
    @Autowired
    private IProcessEntityService<Customer> customerService;
    @Autowired
    private ITaskDataEntityService<EditCustomerTaskData> editCustomerTaskDataService;
	
	@Override
	@Transactional
	public void completeTask(String taskId, Customer linkedObject,
			EditCustomerTaskData taskData, String user) {
    	editCustomerTaskDataService.merge(taskData);
        // save new values
    	if (!taskData.getNewDescription().equals(taskData.getOldDescription()) && !StringUtil.isEmpty(taskData.getNewDescription())) {
    		linkedObject.setDescription(taskData.getNewDescription());
    	}
    	if (!taskData.getNewIco().equals(taskData.getOldIco()) && !StringUtil.isEmpty(taskData.getNewIco())) {
    		linkedObject.setIco(taskData.getNewIco());
    	}
    	if (!taskData.getNewDic().equals(taskData.getOldDic()) && !StringUtil.isEmpty(taskData.getNewDic())) {
    		linkedObject.setDic(taskData.getNewDic());
    	}
    	if (!taskData.getNewIcdph().equals(taskData.getOldIcdph()) && !StringUtil.isEmpty(taskData.getNewIcdph())) {
    		linkedObject.setIcdph(taskData.getNewIcdph());
    	}
    	if (!taskData.getNewName().equals(taskData.getOldName()) && !StringUtil.isEmpty(taskData.getNewName())) {
    		linkedObject.setName(taskData.getNewName());
    	}
    	customerService.merge(linkedObject);
    	taskService.setAssignee(taskId, user);
    	taskService.complete(taskId);		
	}

}
